package cn.iocoder.yudao.module.member.statistics.convert;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.DataComparison;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.bo.RegionNode;
import cn.iocoder.yudao.module.api.pay.statistics.dto.PayWalletRechargeSummaryRespDTO;
import cn.iocoder.yudao.module.api.trade.statistics.dto.TradeOrderAreaStatisticsRespDTO;
import cn.iocoder.yudao.module.member.statistics.pojo.vo.MemberAnalyseDataRespVO;
import cn.iocoder.yudao.module.member.statistics.pojo.vo.MemberAnalyseRespVO;
import cn.iocoder.yudao.module.member.statistics.pojo.vo.MemberAreaStatisticsRespVO;
import cn.iocoder.yudao.module.member.statistics.pojo.vo.MemberSummaryRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 用户统计 Convert
 *
 * @author owen
 */
@Mapper
public interface MemberStatisticsConvert {

    MemberStatisticsConvert INSTANCE = Mappers.getMapper(MemberStatisticsConvert.class);

    default List<MemberAreaStatisticsRespVO> convertList(List<RegionNode> regionNodes,
                                                         Map<Integer, Integer> userCountMap,
                                                         Map<Integer, TradeOrderAreaStatisticsRespDTO> orderCountMap) {
        return CollectionUtils.convertList(regionNodes, regionNode -> {
            int regionId = RegionUtils.convertRegionCodeToId(regionNode.getCode());
            TradeOrderAreaStatisticsRespDTO orderVo = Optional.ofNullable(orderCountMap.get(regionId))
                    .orElseGet(TradeOrderAreaStatisticsRespDTO::new);
            MemberAreaStatisticsRespVO respVO = new MemberAreaStatisticsRespVO();
            respVO.setAreaId(regionId);
            respVO.setAreaName(regionNode.getName());
            respVO.setUserCount(MapUtil.getInt(userCountMap, regionId, 0));
            respVO.setOrderCreateUserCount(ObjUtil.defaultIfNull(orderVo.getOrderCreateUserCount(), 0));
            respVO.setOrderPayUserCount(ObjUtil.defaultIfNull(orderVo.getOrderPayUserCount(), 0));
            respVO.setOrderPayPrice(ObjUtil.defaultIfNull(orderVo.getOrderPayPrice(), 0));
            return respVO;
        });
    }

    MemberSummaryRespVO convert(PayWalletRechargeSummaryRespDTO rechargeSummary, Integer rechargePrice, Integer expensePrice);

    MemberAnalyseRespVO convert(Integer visitUserCount, Integer orderUserCount, Integer payUserCount, int atv,
                                DataComparison<MemberAnalyseDataRespVO> comparison);

}
